//
//  TagView.m
//  Wallpaper
//
//  Created by ytkjs2 on 2019/10/11.
//  Copyright © 2019 ytk. All rights reserved.
//

#import "TagView.h"

@interface TagView ()
{
    int popularHeight;
    UIView * historyView;
}
@property (nonatomic, strong) NSMutableArray * btnsArrayM;

@end

@implementation TagView

-(void)setTagView:(NSArray *)arr
{
    [self tagOffsetY:45 withData:arr];
}


-(CGFloat)tagOffsetY:(CGFloat)y withData:(NSArray *)arr
{
    int sumWidth = 0;
    int row = 0;    // 行数
    int maxY = 0;
    if (y != 45) {
        [historyView removeFromSuperview];
        historyView = [[UIView alloc]init];
    }
    
    for (int i = 0; i< arr.count; i++)
    {
        UIButton *btn = [[UIButton alloc] init];
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitleColor:[UIColor blackColor] forState:0];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [btn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [btn.layer setMasksToBounds:YES];
        [btn.layer setCornerRadius:10];
        [btn.layer setBorderColor:[UIColor grayColor].CGColor];
        [btn.layer setBorderWidth:1.0];
        if (y == 45) {
            btn.tag = i;
        }
        else
            btn.tag = i + 1000;
        
        
        [btn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
        
        CGFloat marginX = 5;
        CGFloat marginY = 8;
        
        CGFloat btnWidth = [self labelGetWidthWithLabel:arr[i] maxHeight:21 font:13] + marginX * 2;
        CGFloat btnHeight = 21 ;
        
        btn.frame = CGRectMake(0, 0, btnWidth, btnHeight);
        
        if (sumWidth + btnWidth>= rScreenWidth - 40) {
            sumWidth = 0;
            row ++;
        }
        if (y == 45) {
            btn.center = CGPointMake(btnWidth/2 + sumWidth, 10 + (21 + marginY) * row + 45);
        }
        else
            btn.center = CGPointMake(btnWidth/2 + sumWidth, 10 + (21 + marginY) * row);
        
        
        sumWidth += (btnWidth + marginX);
        
        if (i == arr.count - 1)
        {
            self.frame = CGRectMake(20, 0, rScreenWidth - 40, CGRectGetMaxY(btn.frame) + 20);
            maxY = CGRectGetMaxY(self.frame);
        }
        if (y == 45) {
            [self addSubview:btn];
        }
        else
        {
            [historyView addSubview:btn];
            [self addSubview:historyView];
        }
    }
    if (y != 45) {
        [historyView setFrame:CGRectMake(0, y, rScreenWidth - 40, maxY)];
        self.frame = CGRectMake(20, 0, rScreenWidth - 40, CGRectGetMaxY(historyView.frame) + 20);
    }
    return maxY;
}

-(void)refreshHistory:(NSArray *)arr
{
    [self tagOffsetY:popularHeight withData:arr];
}


-(CGFloat)labelGetWidthWithLabel:(NSString *)label maxHeight:(CGFloat)height font:(CGFloat)font
{
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:font],};
    CGSize textSize = [label boundingRectWithSize:CGSizeMake(300, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    
    return textSize.width;
}

-(void)btnClickAction:(id)sender
{
    [self.controller performSelector:@selector(btnClickAction:) withObject:sender];
}

@end
